package com.cssw.fyzb.web.controller;

import com.cssw.fyzb.base.web.context.UserContext;
import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.web.dto.FeedbackDto;
import com.cssw.fyzb.web.dto.UserDto;
import com.cssw.fyzb.web.service.AppealService;
import com.cssw.fyzb.web.service.CmfGiftService;
import com.cssw.fyzb.web.util.SplitUtil;
import com.cssw.fyzb.web.vo.AppealVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiModel("申诉列表")
@RestController
@RequestMapping("/appeal")
public class AppealController {

    @Autowired
    AppealService appealService;

    @ApiOperation(value = "申诉列表", response = AjaxPage.class)
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "search", value = "搜索框", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "appealTime", value = "注册时间", paramType = "query", dataType = "string", required = false),
    })

    public AjaxPage<AppealVo> list(int page, int limit, String search, String appealTime) {
        Map param = new HashMap();
        if (search != null) {
            String regex = "^[1]\\d{10}$";
            String number = "[0-9]*";
            if (search.matches(regex)) param.put("mobile", search);
            else if (search.matches(number)) param.put("userId", search);
            else param.put("nickName", search);
        }
        param.put("appealTime", appealTime);
        return appealService.list(page, limit, param);
    }


    @ApiOperation(value = "申诉详情", response = AjaxPage.class)
    @RequestMapping(value = "/getDetails", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appealId", value = "主键标识", paramType = "query", dataType = "string", required = true),
    })
    public AjaxData getDetails(Long appealId) {
        return appealService.getDetails(appealId);
    }

    @ApiOperation(value = "答复", response = AjaxData.class)
    @PostMapping(value = "/feedback")
    public AjaxData feedback(@RequestBody FeedbackDto dto) {
        boolean result = appealService.update(dto.getAppealId(), dto.getFeedback(), UserContext.getUserId());
        if (result) return AjaxData.success();
        else return AjaxData.failure();
    }

}